
import user from '@/common/user.js'
import login from '@/common/login.js'
// 全局请求封装
export default (url, method,data) => {
	// console.log('%c请求拦截：', ' background:orange', data);

	return new Promise((resolve, reject) => {
		uni.request({
			header: {
				'token':user.state.token,
				'userId':user.state.userId
			},
			url: user.maiApiUrl + url,
			method: method,
			data:data,
			success(response) {
				// console.log('%c响应拦截：', ' background:green', response);
				if (response.data.code === 33000) {
					uni.showToast({
						icon: "none",
						duration: 2000,
						title: '登入超时！正在重新登入！'
					});
					login.Login()
					setTimeout(()=>{
						uni.switchTab({
							url:'/pages/index/index'
						})
					},2000)
					
				}
				if (response.data.code != 0) {
					uni.showToast({
						icon: "none",
						duration: 2000,
						title: response.data.msg
					});
				}

				resolve(response.data);
			},
			fail(err) {
				uni.showToast({
					icon: "none",
					title: '服务响应失败'
				});
				console.error(err);
				reject(err);
			}
		});
	});
};